<template>
  <div v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
    <el-row>
      <el-col :span="24">
        <div class="tooltip">
          <el-tooltip class="item" effect="dark" content="添加网盘" placement="bottom">
            <el-button type="primary" icon="el-icon-plus" circle @click="dialogFormVisible = true"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-dialog
        title="添加网盘"
        :visible.sync="dialogFormVisible"
        :close-on-press-escape="false"
        :modal="true"
        width="30%"
        :close-on-click-modal="false"
      >
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item label="网盘名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="存储空间">
            <el-select v-model="form.bucket_name" placeholder="存储空间">
              <el-option
                v-for="item in this.$store.state.storage.list"
                :key="item.Id"
                :label="item.bucket_name"
                :value="item.bucket_name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="addNetworkDisk('form')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <ul class="folder-box">
      <li v-for="item in diskInfos" :key="item.catalogue_id" class="folder-box-li">
        <router-link
          :to="{path:'file',query:{
            id:item.catalogue_id,
          bucketid:item.bucket_id,
          bucketname:item.bucket_name,
          rootdir:item.catalogue_name}}"
        >
          <img
            src="../../assets/static/image/folder.png"
            alt
            width="70"
            v-if="showImg"
            @contextmenu.prevent="openMenu($event,item)"
          />
        </router-link>
        <!-- {{ item }} -->
        <span>{{ item.catalogue_name }}</span>
      </li>
      <!-- <vue-context-menu></vue-context-menu> -->
    </ul>
    <!-- 右击菜单 -->
    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
      <li @click="handleDelete(rightClickItem)">删除</li>
      <li @click="handleDetails(rightClickItem)">详情信息</li>
    </ul>

    <!-- 详情弹出框 -->
    <el-dialog
      title="网盘详情信息"
      :visible.sync="dialogDetails"
      width="50%"
      :modal-append-to-body="true"
      :close-on-click-modal="false"
    >
      <!-- <span>{{ detail }}</span> -->
      <el-descriptions class="margin-top" :column="3" :size="size" border>
        <el-descriptions-item>
          <template slot="label">
            <!-- <i class="el-icon-folder"></i> -->
            存储空间名称
          </template>
          {{ detail.bucket_name }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <!-- <i class="el-icon-user"></i> -->
            网盘名称
          </template>
          {{ detail.catalogue_name }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <!-- <i class="el-icon-user"></i> -->
            创建用户
          </template>
          {{ detail.user_id }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <!-- <i class="el-icon-user"></i> -->
            创建时间
          </template>
          {{ detail.create_at }}
        </el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>
  </div>
</template>

<script>
import {
  CatalogueCreate,
  CatalogueDel,
  CatalogueDetail,
  HomeCatalogue
} from "@/utils/http";
export default {
  data() {
    return {
      // 存储空间列表对象
      option: [],
      diskInfos: [{}],
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "90px",
      Limit: 1,
      Page: 10,
      rules: {
        name: [{ required: true, message: "请输入网盘名称", trigger: "blur" }],
        region: [{ required: true, message: "请选择存储空间", trigger: "blur" }]
      },
      showImg: false,
      loading: false,
      matchMediavisible: false,
      visible: false,
      top: 0,
      left: 0,
      detail: {}, // 详情数据
      dialogDetails: false, // 详情弹出框
      size: "medium" //Descriptions 描述列表大小
    };
  },
  // 右击事件
  watch: {
    visible(value) {
      if (value) {
        document.body.addEventListener("click", this.closeMenu);
      } else {
        document.body.removeEventListener("click", this.closeMenu);
      }
    }
  },

  methods: {
    // 右击事件
    openMenu(e, item) {
      this.rightClickItem = item;
      var x = e.pageX;
      var y = e.pageY;
      this.top = y;
      this.left = x;
      this.visible = true;
    },
    closeMenu() {
      this.visible = false;
    },
    // 首页数据加载
    home_catalogue() {
      this.loading = true;

      HomeCatalogue()
        .then(res => {
          if (res.data.code == 200) {
            this.showImg = true;
            this.diskInfos = res.data.data;
            this.$store.commit("setDisk", res.data.data);
            this.loading = false;
          } else {
            this.loading = false;
            this.diskInfos = [];
            this.$message({
              message: "没有相关数据",
              type: "warning",
              center: true
            });
            this.loading = false;
          }
        })
        .catch(err => {
          this.$message.error(err);
          this.loading = false;
        });
    },

    // 删除
    handleDelete(rightClickItem) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let tmp = [];
          // this.loading = true;
          let cname = { catalogue_name: rightClickItem.catalogue_name,catalogue_id: rightClickItem.catalogue_id };
          tmp.push(cname)
          let query = {
            bucket_name: rightClickItem.bucket_name,
            bucket_id: rightClickItem.bucket_id,
            catalogue_name: tmp,
            // catalogue_id: rightClickItem.catalogue_id
          };
          CatalogueDel(query)
            .then(res => {
              if (res.data.code == 200) {
                this.loading = false;
                this.$message({
                  message: res.data.data,
                  type: "success",
                  center: true
                });
                this.home_catalogue();
              } else {
                this.loading = false;

                this.$message({
                  message: res.data.message,
                  type: "warning",
                  center: true
                });
              }
            })
            .catch(err => {
              this.$message.error(err);
            });
        })
        .catch(() => {});
    },

    // 添加网盘
    addNetworkDisk(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          for (let i = 0; i < this.$store.state.storage.list.length; i++) {
            if (
              this.$store.state.storage.list[i].bucket_name ==
              this.form.bucket_name
            ) {
              let query = {
                catalogue_name: this.form.name,
                bucket_id: this.$store.state.storage.list[i].Id,
                bucket_name: this.form.bucket_name
              };
              CatalogueCreate(query)
                .then(res => {
                  if (res.data.code == 200) {
                    this.loading = false;
                    this.$message({
                      message: "添加成功",
                      type: "success",
                      center: true
                    });
                    this.form = {};
                    this.dialogFormVisible = false;
                    this.home_catalogue();
                  } else {
                    this.$message({
                      message: res.data.message,
                      type: "warning",
                      center: true
                    });
                    this.form = {};
                    this.dialogFormVisible = false;
                  }
                })
                .catch(err => {
                  this.$message.error(err);
                });
            }
          }
        } else {
          return false;
        }
      });
    },
    // 详情
    handleDetails(rightClickItem) {
      this.loading = true;
      // console.log(rightClickItem);
      let query = {
        bucket_name: rightClickItem.bucket_name,
        bucket_id: rightClickItem.bucket_id,
        catalogue_id: rightClickItem.catalogue_id,
        catalogue_name: rightClickItem.catalogue_name
      };
      CatalogueDetail(query)
        .then(res => {
          if (res.data.code == 200) {
            this.detail = res.data.data.list;
            // console.log(res.data.data)
            this.dialogDetails = true;
            this.loading = false;
          } else {
            this.loading = false;

            this.$message({
              message: res.data.message,
              type: "warning",
              center: true
            });
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 取消添加网盘按钮事件
    cancel() {
      this.dialogFormVisible = false;
      this.form = {};
    }
  },
  mounted() {
    this.home_catalogue();
  }
};
</script>

<style lang="less" scoped>
.folder-box {
  display: flex;
  flex-wrap: wrap;
  cursor: pointer;
  > li {
    margin-left: 18px;
    margin-right: 26px;
    text-align: center;
    font-size: 14px;
  }
}
.el-row {
  position: relative;
}
.tooltip {
  position: absolute;
  right: 0;
}

// 右击事件样式
.contextmenu {
  margin: 0;
  background: #fff;
  z-index: 3000;
  position: absolute;
  list-style-type: none;
  padding: 5px 0;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  text-align: center;
  color: #333;
  box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
}

.contextmenu li {
  margin: 0;
  padding: 7px 16px;
  cursor: pointer;
}

.contextmenu li:hover {
  background: #eee;
}
</style>